package com.lxw.station.service;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Random;

public class ArticleDetail {

    // 创建UUID
    private static String createUuid() {
        Random rand = new SecureRandom();
        StringBuilder uuid = new StringBuilder();
        String charList = "abcdefghijklmnopqrstuvwxyz0123456789";
        for (int i = 0; i < 36; i++) {
            if (i == 8 || i == 13 || i == 18 || i == 23) {
                uuid.append("-");
            } else if (i == 14) {
                uuid.append("4");
            } else {
                uuid.append(charList.charAt(rand.nextInt(charList.length())));
            }
        }
        return uuid.toString();
    }

    // 获取签名
    private static String getSign(String uuid, String url) throws NoSuchAlgorithmException, InvalidKeyException {
        URI uri = URI.create(url);
        uuid = "54c97b13-1a48-4125-bf7b-f860b43acb0d";
        String ekey = "9znpamsyl2c7cdrr9sas0le9vbc3r6ba";
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(ekey.getBytes(), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        //String     ss = "GET\\napplication/json, text/plain, */*\\n\\n\\n\\nx-ca-key:203803574\\nx-ca-nonce:c22e7c32-3cbd-42df-a487-511e78038dae\\n/blog-console-api/v1/user/info";
        String     ss = "GET\napplication/json, text/plain, */*\n\n\n\nx-ca-key:203803574\nx-ca-nonce:c22e7c32-3cbd-42df-a487-511e78038dae\n/blog-console-api/v1/user/info";
        //String     to_enc = "GET\napplication/json, text/plain, */*\n\n\n\nx-ca-key:203803574\nx-ca-nonce:"+uuid+"\n/blog-console-api/v1/editor/getArticle";
        String     to_enc = "GET\napplication/json, text/plain, */*\n\n\n\nx-ca-key:203803574\nx-ca-nonce:"+uuid+"\n/blog-console-api/v1/editor/getArticle"+ (uri.getQuery() != null ? "?" + uri.getQuery() : "");
//        String to_enc = "GET\n*/*\n\n\n\nx-ca-key:203803574\nx-ca-nonce:" + uuid + "\n" + uri.getPath() + (uri.getQuery() != null ? "?" + uri.getQuery() : "");
        byte[] bytes = sha256_HMAC.doFinal(to_enc.getBytes());
        return Base64.getEncoder().encodeToString(bytes);
    }

    // 保存文件
    private static void saveToFile(String filename, String content) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//        String url = "https://wshuo.blog.csdn.net/";
        String url = "https://bizapi.csdn.net/blog-console-api/v1/editor/getArticle?id=136523025&model_type=";
//        String url = "https://blog.csdn.net/";
        String uuid = createUuid();
        System.out.println(uuid);
        String sign = getSign(uuid, url);
        System.out.println(sign);



    }
}

